package com.tcwlkj.uuService.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.tcwlkj.uuService.R;

import java.util.List;

/**
 * Created by Administrator on 2017/7/7 0007.
 */

public class GalleryAdapter extends RecyclerView.Adapter<GalleryAdapter.ViewHolder> {

    /**
     * ItemClick的回调接口
     * @author zhy
     *
     */
    public interface OnItemClickLitener
    {
        void onItemClick(View view, int position);
    }

    private OnItemClickLitener mOnItemClickLitener;

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener)
    {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    private LayoutInflater mInflater;
    private List<String> mBigDatas;
    private List<String> mSmallData;
    private int  itemWidth;
    private int selectedPosition = 0; //默认一个参数

    public GalleryAdapter(Context context, List<String> datats , List<String> textData, int width)
    {
        mInflater = LayoutInflater.from(context);
        mBigDatas = datats;
        mSmallData= textData;
        itemWidth=width;
    }

    public class ViewHolder extends RecyclerView.ViewHolder
    {
        public ViewHolder(View arg0)
        {
            super(arg0);
        }

        TextView tv_big_title;
        TextView tv_small_title;
        View       mView;
    }

    @Override
    public int getItemCount()
    {
        return mBigDatas.size();
    }

    /**
     * 创建ViewHolder
     */
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i)
    {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_honors_titile, viewGroup, false);
        view.getLayoutParams().width = itemWidth;
        ViewHolder viewHolder = new ViewHolder(view);

        viewHolder.tv_big_title = (TextView) view
                .findViewById(R.id.tv_big_title);
        viewHolder.tv_small_title =(TextView)view.findViewById(R.id.tv_small_title);
        viewHolder.mView =(View)view.findViewById(R.id.item_recycler_view);
        return viewHolder;
    }

    /**
     * 设置值
     */
    @Override
    public void onBindViewHolder(final ViewHolder viewHolder, final int i) {

        viewHolder.tv_big_title.setText(mBigDatas.get(i));
        viewHolder.tv_small_title.setText(mSmallData.get(i));
        viewHolder.itemView.setSelected(selectedPosition == i);
        if (selectedPosition == i) {
            viewHolder.mView.setVisibility(View.VISIBLE);
        } else {
            viewHolder.mView.setVisibility(View.INVISIBLE);
        }
        //如果设置了回调，则设置点击事件
        if (mOnItemClickLitener != null)
        {
            viewHolder.itemView.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    mOnItemClickLitener.onItemClick(viewHolder.itemView, viewHolder.getAdapterPosition());
                    selectedPosition = i; //选择的position赋值给参数，
                    notifyDataSetChanged();
                }
            });
        }
    }
}